﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Cart
/// </summary>
public class Cart
{
	public Cart()
	{
        dt = new DataTable();
        dt.Columns.Add("Hinhanh");
        dt.Columns.Add("TenSP");
        dt.Columns.Add("Soluong");
        dt.Columns.Add("Gia");
        dt.Columns.Add("tonggia");
        dt.Columns.Add("MaSP");
	}
    AccessData ad = new AccessData();
    DataTable dt;
 
    bool kiemtra = false;
    //public string LayTenSP(string masp)
    //{
    //    da = new SqlDataAdapter("select * from SanPham Where MaSP='" + masp + "'", ad.GetConnect());
    //    da.Fill(dt);
    //    string namesp = dt.Rows[0]["TenSP"].ToString();
    //    return namesp;
    //}
    //public double LayGiaSP(string masp)
    //{
    //    da = new SqlDataAdapter("select * from SanPham Where MaSP='" + masp + "'", ad.GetConnect());
    //    da.Fill(dt);
    //    return (double)dt.Rows[0]["Gia"];
    //}
    public DataTable giohang(int ma_sp)
    {
        SqlConnection connDB = ad.GetConnect();
        SqlCommand cmd = new SqlCommand("SANPHAM_Giohang", connDB);
        connDB.Open();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Ma_SP", System.Data.SqlDbType.Int).Value = ma_sp;
        cmd.ExecuteNonQuery();
        DataTable dt = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = cmd;
        adapter.Fill(dt);
        return dt;
    }

    public DataTable setTable()
    {
        return this.dt;
    }

    public DataTable dienVaoBang(DataTable tb, string hinh, string tensp, double soluong, double gia, string masp)
    {
        if (tb.Rows.Count != 0)
        {
            foreach (DataRow dr in tb.Rows)
            {
                if (dr[1].ToString().Equals(tensp))
                {
                    dr[2] = double.Parse(dr[2].ToString()) + 1;
                    dr[4] = double.Parse(dr[2].ToString()) * double.Parse(dr[3].ToString());
                    kiemtra = true;
                }
            }
            if (!kiemtra)
            {
                DataRow dtr = tb.NewRow();
                dtr[0] = hinh;
                dtr[1] = tensp;
                dtr[2] = soluong;
                dtr[3] = gia;
                dtr[4] = soluong * gia;
                dtr[5] = masp;
                tb.Rows.Add(dtr);
            }
        }
        else
        {
            DataRow dtr = tb.NewRow();
            dtr[0] = hinh;
            dtr[1] = tensp;
            dtr[2] = soluong;
            dtr[3] = gia;
            dtr[4] = soluong * gia;
            dtr[5] = masp;
            tb.Rows.Add(dtr);
        }
        return tb;
    }

    public DataTable xoaDong(DataTable tb, string tensp)
    {
        foreach (DataRow dr in tb.Rows)
        {
            if (dr[1].ToString().Equals(tensp))
            {
                tb.Rows.Remove(dr);
                break;
            }
        }
        return tb;
    }

    public DataTable capNhatlai(DataTable tb, double soluong, string tensp)
    {
        dt = tb;
        foreach (DataRow dr in dt.Rows)
        {
            if (tensp.Trim().Equals(dr[1].ToString().Trim()))
            {
                dr[2] = soluong;
                dr[4] = double.Parse(dr[3].ToString()) * soluong;
            }
        }
        return dt;
    }

    public SqlDataReader laysoluong(string tensp)
    {
        SqlConnection connDB = ad.GetConnect();
        string sql = string.Format("select * from SANPHAM where TenSP = {0}",tensp);
        SqlCommand cmd = new SqlCommand("",connDB);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Ten_SP",System.Data.SqlDbType.VarChar).Value = tensp;
        SqlDataReader reader;
        connDB.Open();
        reader= cmd.ExecuteReader();
        return reader;
    }
}